﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxGridView;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class RAEmailSender : ICPRelationshipPage
{
    public String m_sxRecentEmail = String.Empty;
    #region Properties
    
    
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Change Email Sender";
        if (!IsPostBack && !IsCallback)
        {
            m_sxRecentEmail = InitRecentEmail();
        }
    }
    protected void cbkRA_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        if (e.Parameter.ToUpper().StartsWith("CHANGE"))
        {
            e.Result = HandleChange(e.Parameter);
        }
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    protected String InitRecentEmail()
    {
        String sxOutput = String.Empty;
        String sxEmail = String.Empty;
        sxEmail = DBQuery.EmailSenderTable.GetRecentEmail();
        if (!String.IsNullOrEmpty(sxEmail))
        {
            sxOutput = sxEmail;
        }
        return sxOutput;
    }
    protected String HandleChange(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        String sxReturn = String.Empty;
        String sxNewEmail = sxSplitted[1];
        int iNewId = DBQuery.EmailSenderTable.NewEmailSender(sxNewEmail);
        if (iNewId > 1)
        {
            DBQuery.EmailSenderTable.UpdateOldEmail(iNewId - 1);
        }
        sxReturn = "CHANGE" + KGlobal.DELIMITER + sxNewEmail;
        return sxReturn;
    }
    #endregion Custom Methods
}
